{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "import tensorflow as tf\r\n",
    "from tensorflow.examples.tutorials.mnist import input_data\r\n",
    "tf.compat.v1.disable_eager_execution()\r\n",
    "#下载数据集\r\n",
    "mnist = input_data.read_data_sets('MNIST_data',one_hot=True)\r\n",
    "#每个批次的大小\r\n",
    "batch_size = 100\r\n",
    "#计算一共有多个批次  //batch_size\r\n",
    "n_batch = mnist.train.num_examples\r\n",
    "x = tf.compat.v1.placeholder(tf.float32,[None,784])\r\n",
    "#0-9 10个数字\r\n",
    "y = tf.compat.v1.placeholder(tf.float32,[None,10])\r\n",
    "#创建一个简单的神经网络\r\n",
    "W = tf.Variable(tf.zeros([784,10]))\r\n",
    "b = tf.Variable(tf.zeros([10]))\r\n",
    "\r\n",
    "prediction = tf.nn.softmax(tf.matmul(x,W)+b)\r\n",
    "#二次代阶函数\r\n",
    "# loss = tf.reduce_mean(tf.square(y-prediction))\r\n",
    "#交叉熵代价函数\r\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\r\n",
    "#使用梯度下降法\r\n",
    "train_step = tf.compat.v1.train.GradientDescentOptimizer(0.2).minimize(loss)\r\n",
    "#初始化变量\r\n",
    "init = tf.compat.v1.global_variables_initializer()\r\n",
    "#\r\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))\r\n",
    "#求准确率\r\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\r\n",
    "\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    sess.run(init)\r\n",
    "    for epoch in range(21):\r\n",
    "        for batch in range (n_batch):\r\n",
    "            batch_xs,batch_ys = mnist.train.next_batch(batch_size)\r\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})\r\n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\r\n",
    "        print('Iter' +str(epoch)+',Testing Accuray' +str(acc))\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter0,Testing Accuray0.9284\n",
      "Iter1,Testing Accuray0.9297\n",
      "Iter2,Testing Accuray0.9291\n",
      "Iter3,Testing Accuray0.9296\n",
      "Iter4,Testing Accuray0.9302\n",
      "Iter5,Testing Accuray0.9297\n",
      "Iter6,Testing Accuray0.9296\n",
      "Iter7,Testing Accuray0.9297\n",
      "Iter8,Testing Accuray0.9302\n",
      "Iter9,Testing Accuray0.9302\n",
      "Iter10,Testing Accuray0.9295\n",
      "Iter11,Testing Accuray0.9293\n",
      "Iter12,Testing Accuray0.9296\n",
      "Iter13,Testing Accuray0.9293\n",
      "Iter14,Testing Accuray0.9292\n",
      "Iter15,Testing Accuray0.9292\n",
      "Iter16,Testing Accuray0.9293\n",
      "Iter17,Testing Accuray0.9291\n",
      "Iter18,Testing Accuray0.929\n",
      "Iter19,Testing Accuray0.9286\n",
      "Iter20,Testing Accuray0.9285\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "import tensorflow as tf\r\n",
    "import matplotlib.pyplot as plt\r\n",
    "\r\n",
    "print(tf.__version__)\r\n",
    "mint=tf.keras.datasets.mnist\r\n",
    "(x_,y_),(x_1,y_1)=mint.load_data()\r\n",
    "\r\n",
    "plt.imshow(x_[0], cmap=\"binary\")\r\n",
    "plt.show()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "2.6.0\n"
     ]
    },
    {
     "output_type": "display_data",
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2F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/rYJeZqyePW8t/ZzWy4pP+V9O+SeiXtlbTQ3f+npYNUYWY9kkruXvgbMMxslqR/SNrk7tdntz0p6ZS7P5H9RznG3f+zTWZbK+kfRS/jna1W1Nl/mXFJ8yTdpwIfu8Rc/6EWPG5FHNmnS/rA3T9y99OS/iBpbgFztD13f1PSqXNunitpY3Z9o/r+sbRcldnagrsfc/d3sutfSDq7zHihj11irpYoouzjJf213/e9aq/13l3Sn81sn5ktKXqYAVzp7sekvn88kq4oeJ5z1VzGu5XOWWa8bR67epY/b1QRZR9oKal2Ov83092nSbpD0tLs6SoGZ1DLeLfKAMuMt4V6lz9vVBFl75U0od/3P5b0SQFzDMjdP8kuT0japvZbivr42RV0s8sTBc/z/9ppGe+BlhlXGzx2RS5/XkTZ90q61sx+YmYjJS2QtL2AOb7HzEZlL5zIzEZJmqP2W4p6u6TF2fXFkl4pcJbvaJdlvKstM66CH7vClz9395Z/SbpTfa/IfyhpdREzVJnrXyT9Jfs6WPRskjar72nd1+p7RvSApH+StEvSkexybBvN9t+S3pPUrb5idRY027+p71fDbkn7s687i37sEnO15HHj7bJAELyDDgiCsgNBUHYgCMoOBEHZgSAoOxAEZQeC+D+ypTV9clByEAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     }
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.8",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}